package com.xcdh.ittools.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONBeanParser;
import cn.hutool.json.JSONUtil;
import com.api.ittools.query.GeneratorQuery;
import com.api.ittools.vo.ProjectVo;
import com.api.ittools.vo.TablesVo;
import com.api.ittools.vo.TplVo;
import com.xcdh.ittools.service.GeneratorCodeService;
import com.xcdh.ittools.service.impl.GeneratorCodeServiceImpl;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@RestController
@RequestMapping("/gen")
public class GeneratorCodeController {

    @Autowired
    private GeneratorCodeService codeService ;

    @RequestMapping("/code")
    public void genCode(@RequestBody GeneratorQuery  query, HttpServletResponse response){
        ZipOutputStream zip = null ;
        ByteArrayOutputStream byteArrayOutputStream = null ;
        try{
            byteArrayOutputStream = new ByteArrayOutputStream();
            ProjectVo projectVo = codeService.buildToolTpl(query);
            zip = new ZipOutputStream(byteArrayOutputStream);
            List<TablesVo> tablesVos = projectVo.getTablesVos();
            for (TablesVo tablesVo : tablesVos) {
                for (TplVo tplVo : tablesVo.getTplVoList()) {
                    zip.putNextEntry(new ZipEntry(tplVo.getFileName()));
                    zip.write(tplVo.getCode().getBytes(StandardCharsets.UTF_8));
                    zip.closeEntry();
                }
            }
            zip.close();
            byte[] byteArray = byteArrayOutputStream.toByteArray();
            response.addHeader("Content-Disposition", "attachment; filename="+projectVo.getProjectName()+".zip");
            response.setContentType("application/zip;charset=UTF-8");
            ServletOutputStream outputStream = response.getOutputStream();
            IOUtils.write(byteArray , outputStream);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try{
                if(zip != null) {  zip.close(); }
                if(byteArrayOutputStream != null) {byteArrayOutputStream.close();}
            }catch (IOException e){

            }

        }
    }


    @GetMapping("/test")
    public void test(HttpServletResponse response , HttpServletRequest request){
        String content1 = "Hello, this is the first string content.";
        String content2 = "Hi, this is the second string content.";
// 创建一个字节数组输出流，用于存储压缩后的数据
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try (ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputStream)) {
            // 添加第一个 ZIP 条目
            zipOutputStream.putNextEntry(new ZipEntry("a\\file1.txt"));
            zipOutputStream.write(content1.getBytes(StandardCharsets.UTF_8));
            zipOutputStream.closeEntry();

            // 添加第二个 ZIP 条目
            zipOutputStream.putNextEntry(new ZipEntry("a\\b\\file2.txt"));
            zipOutputStream.write(content2.getBytes(StandardCharsets.UTF_8));
            zipOutputStream.closeEntry();

            // 关闭 ZIP 输出流
            zipOutputStream.close();
            // 获取压缩后的字节数组


            response.setContentType("application/zip");
            response.addHeader("Content-Disposition", "attachment; filename=test.zip");
            // 这里可以将 zipBytes 存储到文件或进行其他操作
            response.getOutputStream().write(byteArrayOutputStream.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
